import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout.vue'
import Login from '@/views/login.vue'
import userView from '@/views/userView.vue'
import Dashboard from '@/views/layout/dashboard.vue'
import Article from '@/views/layout/article.vue'
import store from '@/store/index'
import UserHome from '@/views/userView/userHome.vue'
import UserArticle from '@/views/userView/userArticle.vue'
import selectArticle from '@/views/userView/selectArticle.vue'

Vue.use(VueRouter)


const router = new VueRouter({
  routes: [
    { path: '/login', component: Login},
    { path: '/user', component: userView,
      children: [
        {path: '/home',component: UserHome},
        {path: '/userarticle/:id', component: UserArticle },
        {path: '/select', name: 'select' ,component: selectArticle }
      ]
     },
    {
      //重新定向
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [
        { path: 'dashboard', component: Dashboard },
        { path: 'article', component: Article }
      ]
    }
  ]
})
//跳转之前判断是否登录
router.beforeEach((to,from,next)=>{
  console.log(store);
  const {token} = store.state.user
  if(to.path !== '/login' && !token){
    return next('/login')
  }
  next()
})

export default router
